﻿using System;
using SPPropertyBagManagement.Code;
using System.Drawing;

namespace SPPropertyBagManagement.Layouts.SPPropertyBagManagement
{
    public partial class SingleEdit : PropertyManagementBasePage
    {
        /// <summary>
        /// Represents the method that handles the Init event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            SetPageTitle("Edit property: " + PropertyName + " | Scope: " + Scope);
            lblPropertyId.Text = PropertyName;

            if (!IsPostBack)
            {
                LoadProperty();
            }
        }

        /// <summary>
        /// Loads the property.
        /// </summary>
        private void LoadProperty()
        {
            Property property;

            if (ObjectId == null)
            {
                property = PropertyManager.GetProperty(Scope, null, null, PropertyName);    
            }
            else
            {
                property = PropertyManager.GetProperty(Scope, ObjectId, ListItemObjectId, PropertyName);    
            }

            if (property == null || !property.SupportsEditing)
            {
                SetEditorState(false);
                return;
            }

            lblPropertyType.Text = property.TypeCode.ToString();
            tbPropertyValue.Text = property.PropertyValue == null ? string.Empty : property.PropertyValue.ToString();
        }

        /// <summary>
        /// Sets the state of the editor.
        /// </summary>
        /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
        private void SetEditorState(bool isEnabled)
        {
            divEditor.Disabled = !isEnabled;
        }

        /// <summary>
        /// Handles the OnClick event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            UpdateProperty();
        }

        /// <summary>
        /// Updates the property.
        /// </summary>
        private void UpdateProperty()
        {
            try
            {
                Property myProp = PropertyManager.GetProperty(Scope, ObjectId, ListItemObjectId, PropertyName);
                myProp.PropertyValue = tbPropertyValue.Text;

                myProp.Update();

                CloseDialog(0, "saveOk", "Property was saved.");
            }
            catch (Exception ex)
            {
                ShowMessage("Save failed: " + ex.Message, true);
            }
        }

        /// <summary>
        /// Shows the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="isError">if set to <c>true</c> [is error].</param>
        protected void ShowMessage(string message, bool isError)
        {
            lblMessage.Text = message;

            if (isError)
            {
                lblMessage.ForeColor = Color.Red;
            }
            else
            {
                lblMessage.ForeColor = Color.Silver;
            }

            lblMessage.Visible = true;
        }

        /// <summary>
        /// Hides the message.
        /// </summary>
        protected void HideMessage()
        {
            lblMessage.Visible = false;
        }
    }
}
